<?php
namespace app\entries\model;
use think\Model;

/**
 *
 */
class Area extends Model
{
    //返回产品名称
    function teacase()
    {
        $res = db('teacase')->select();
        return $res;
    }

    //返回产品名称
    function area()
    {
        $res = db('teaarea')->select();
        return $res;
    }

    //查找产区是否存在
    function findArea($area)
    {
        $res = db('teaarea')->where('area', $area)->find();
        return $res;
    }

    //产区不存在时，先插入产区，获取插入id,再建立关联
    function insertArea($teacase_id, $data, $source)
    {
        $insert=['area'=>$data];
        $res = db('teaarea')->insert($insert);
        $teaarea_id = db('teaarea')->getLastInsID();

        $associate=["teacase_id"=>$teacase_id, "teaarea_id"=>$teaarea_id, 'source'=>$source];
        db('case_area')->insert($associate);
        return 1;
    }

    function caseArea($teacase_id)
    {
        $res = db('case_area')->field("teaarea_id, display, source")->where("teacase_id", $teacase_id)->select();
        return $res;
    }

    //解除关联
    function delArea($tid, $aid)
    {
        $contition = ['teacase_id'=>$tid, 'teaarea_id'=>$aid];
        db('case_area')->where($contition)->delete();
        // $res = db('case_area')->where('teaarea_id',$aid)->find();
        // if(!$res){
        //     db('teaarea')->where('id', $aid)->delete();
        // }
    }

    //判断是否已经建立联系
    function findAreaCase($tid, $aid)
    {
        $condition=['teacase_id'=>$tid, 'teaarea_id'=>$aid];
        $res = db('case_area')->where($condition)->find();
        return $res;
    }


    //建立一一对应关系
    function create_relate($tid, $aid, $source)
    {
        $insert=['teacase_id'=>$tid, 'teaarea_id'=>$aid, 'source'=>$source];
        db('case_area')->insert($insert);
    }






}
?>